<template>
  <div class="approve-manage">
    <div class="base-table">
      <div class="action">
        <el-button type="primary" @click="afterAllRead">一键已读</el-button>
        <el-button type="danger" @click="deleteAllApprove">一键清除</el-button>
      </div>
      <el-table
          :data="approveList"
          row-key="_id"
          :tree-props="{ children: 'children' }"
          stripe
      >
        <el-table-column
            v-for="item in columns"
            :key="item.prop"
            v-bind="item"
        ></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="primary" :disabled="scope.row.isRead" @click.once="afterRead(scope.row)"
            >已读</el-button
            >
            <el-button size="mini" @click="readUser(scope.row)"
            >查看</el-button
            >
            <el-button
                size="mini"
                type="danger"
                @click.once="deleteOne(scope.row)"
            >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>


      <!-- 用户信息弹窗-->
      <el-dialog title="查看用户信息" v-model="showUserModal">
        <el-form label-width="100px" :model="userForm" :rules="rules">
          <el-form-item label="用户名">
            {{userForm.userName}}
          </el-form-item>
          <el-form-item label="用户邮箱">
            {{userForm.userEmail}}
          </el-form-item>
          <el-form-item label="手机号">
            {{userForm.mobile}}
          </el-form-item>
          <el-form-item label="岗位">
            {{userForm.job}}
          </el-form-item>
          <el-form-item label="状态">
            {{userForm.state}}
          </el-form-item>
          <el-form-item label="系统角色">
            <template v-for="item in roleName">
              {{item}},
            </template>
          </el-form-item>
          <el-form-item label="部门">
            <template v-for="item in deptName">
              {{item}},
            </template>
          </el-form-item>
        </el-form>
        <template #footer>
        <span class="dialog-footer">
          <el-button @click="showUserModal = false" type="primary">确认</el-button>
        </span>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
export default {
  name: "approve",
  data() {
    return {
      showUserModal:false,
      roleName:[],
      deptName:[],
      userForm:{},
      //登陆用户信息
      loginUser:this.$store.state.userInfo,
      // 信息列表
      approveList:[],
      //表头
      columns: [
        {
          label: "操作名称",
          prop: "approveName",
        },
        {
          label: "发送人",
          prop: "sendName",
        },
        {
          label: "状态",
          prop: "isRead",
          formatter(row, column, value) {
            return {
              true: "已读",
              false: "未读",
            }[value];
          },
        },
        {
          label: "发布时间",
          prop: "sendTime",
        },
      ],
    };
  },
  //初始化函数，获取所有菜单
  mounted() {
    this.getMenuList()
  },
  methods: {
    async readUser(row){
      let userId=row.receiveId;
      let res=await this.$api.getApproveUser({userId})
      console.log(res[0]);
      let roleId=res[0].roleList;
      let deptId=res[0].deptId;
      for(let value of roleId){
        let _id=value;
        let res=await this.$api.getRoleName({_id})
        this.roleName.push(res[0].roleName)
      }
      for(let value of deptId){
        let _id=value;
        let res=await this.$api.getDeptName({_id})
        this.deptName.push(res[0].deptName)
      }
      console.log(this.roleName,this.deptName)
      this.userForm=res[0];
      this.showUserModal=true;
    },
    //获取菜单列表的方法
    async getMenuList() {
      try {
        let loginUserId=this.loginUser.userId;
        let params={};
        params.loginUserId=loginUserId
        const list = await this.$api.getApproveList(params);
        this.approveList = list;
        if(this.approveList.length==0){
          alert("消息为空")
        }
      } catch (error) {
        console.error(error);
      }
    },
    // 设置单条信息已读的方法
    async afterRead(row){
      if(row.isRead==false){
        row.isRead=true
      }
      let params={}
      params._id=row._id;
      params.isRead=row.isRead;
      await this.$api.updateApprove(params)
      this.getMenuList()
      window.location.reload();
    },
    // 删除单条信息的方法
    async deleteOne(row){
      let params={}
      params._id=row._id;
      await this.$api.deleteApprove(params)
      this.getMenuList()
      window.location.reload();
    },
    // 一键已读的方法
    async afterAllRead(){
      let loginUserId=this.loginUser.userId;
      let params={};
      params.isRead=true;
      params.loginUserId=loginUserId;
      await this.$api.afterAllRead(params)
      window.location.reload()
      this.getMenuList()
      window.location.reload();
    },
    // 一键删除的方法
    async deleteAllApprove(){
      let loginUserId=this.loginUser.userId;
      let params={};
      params.loginUserId=loginUserId;
      await this.$api.deleteAllApprove(params)
      this.$router.push("/welcome")
      window.location.reload()
      this.getMenuList()
      window.location.reload();
    }
  },
};
</script>
